package com.ruoyi.business.controller;

import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.business.domain.MobileNews;
import com.ruoyi.business.service.IMobileNewsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 资讯数据Controller
 * 
 * @author ruoyi
 * @date 2024-06-14
 */
@RestController
@RequestMapping("/mobile/news")
public class MobileNewsController extends BaseController {
    @Autowired
    private IMobileNewsService mobileNewsService;

    /**
     * 查询资讯数据列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MobileNews mobileNews) {
        startPage();
        List<MobileNews> list = mobileNewsService.selectMobileNewsList(mobileNews);
        return getDataTable(list);
    }

    /**
     * @Author liumaotong
     * @Description 小程序用资讯列表
     * @Date 2024/6/20
     * @Param []
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @RequestMapping("/newsList")
    public AjaxResult newsList(){
        QueryWrapper<MobileNews> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(MobileNews::getReleaseTime);
        return AjaxResult.success(mobileNewsService.list(queryWrapper));
    }



    /**
     * 导出资讯数据列表
     */
    @Log(title = "资讯数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MobileNews mobileNews) {
        List<MobileNews> list = mobileNewsService.selectMobileNewsList(mobileNews);
        ExcelUtil<MobileNews> util = new ExcelUtil<MobileNews>(MobileNews.class);
        util.exportExcel(response, list, "资讯数据数据");
    }

    /**
     * 获取资讯数据详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mobileNewsService.selectMobileNewsById(id));
    }

    /**
     * @Author liumaotong
     * @Description 保存
     * @Date 2024/6/19
     * @Param [mobileNews]
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @RequestMapping("/save")
    public AjaxResult save(@RequestBody MobileNews mobileNews){
        if("1".equals(mobileNews.getStatus())){
            mobileNews.setReleaseTime(new Date());
        }
        mobileNewsService.saveOrUpdate(mobileNews);
        return success();
    }

    /**
     * 删除资讯数据
     */
    @Log(title = "资讯数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mobileNewsService.deleteMobileNewsByIds(ids));
    }


}
